<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\InsuredScheme;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class InsuredSchemeController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];

        $data = $request->data;
        foreach ($data as $k=>$v) {
            if ( $v['scheme_id']) {
                $insured = InsuredScheme::findOrFail($v['scheme_id']);
                $insured->mtime = time();
            } else {
                $insured = new InsuredScheme();
                $insured->ctime = time();
            }
            $insured->company_id = $company_id;
            $insured->city = $v['city'];
            $insured->name = $v['name'];
            $insured->default_base = $v['default_base'];
            $insured->select_base_start = $v['select_base_start'];
            $insured->select_base_end = $v['select_base_end'];
            $insured->percent_or_fixed = $v['percent_or_fixed'];
            $insured->actual_base = $v['actual_base'];
            $insured->company_percent = $v['company_percent'];
            $insured->person_percent = $v['person_percent'];
            $insured->company_fixed_amount = $v['company_fixed_amount'];
            $insured->person_fixed_amount = $v['person_fixed_amount'];
            $insured->company_mantissa_processing = $v['company_mantissa_processing'];
            $insured->person_mantissa_processing = $v['person_mantissa_processing'];
            $insured->company_amount = $v['company_amount'];
            $insured->person_amount = $v['person_amount'];
            $insured->type = $v['type'];
            $insured->uid = $uid;

            $insured->save();
        }


        return showError(Error::ERR_OK,['id'=>$insured->id]);

    }

    public function show(Request $request) {
        $insured = InsuredScheme::findOrFail($request->scheme_id);
        return showError(Error::ERR_OK,['insured'=>$insured]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
        $name = $request->input('name');
        if (!empty($name))
        {
            $where['name'] = $name;
        }

        $where['company_id'] = $company_id;
        $where['status'] = 1;

        $list = InsuredScheme::getList($where);

        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        InsuredScheme::where('scheme_id',$request->scheme_id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }


}
